package com.yaojianfeng.openai.controller;

import jakarta.annotation.Resource;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/chat")
public class OpenAIChatController {

    @Resource(name = "openAiChatModel")
    private OpenAiChatModel chatModel;

    @PostMapping("/chat1/{msg}")
    public String chat1(@PathVariable String msg) {
        return chatModel.call(msg);
    }

    /**
     * 4.0模型 测试 需要使用4.0的API key
     *
     * @param msg
     * @return
     */
    @PostMapping("/chat2/{msg}")
    public ChatResponse chat2(@PathVariable String msg) {
        return chatModel.call(new Prompt(msg));
    }

    /**
     * 3.5模型 测试 可以用免费的API key测试
     *
     * @param msg
     * @return
     */
    @PostMapping("/chat3/{msg}")
    public ChatResponse chat3(@PathVariable String msg) {
        OpenAiChatOptions options = new OpenAiChatOptions();
        options.setModel("gpt-3.5-turbo");
        options.setTemperature(0.9);
        options.setMaxTokens(150);
        return chatModel.call(new Prompt(msg, options));
    }


    @PostMapping("/chat4/{msg}")
    public String chat4(@PathVariable String msg) {
        return chatModel.call(new Prompt(msg)).getResult().getOutput().getContent();
    }

}
